package cn.uce.common.utils;

import cn.uce.common.cache.CcCodeCache;
import cn.uce.common.constants.Constants;
import cn.uce.core.cache.CacheManager;
import cn.uce.core.cache.base.ICache;
import cn.uce.utils.StringUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

public class CodeCacheUtils {

    //客户编码前缀
    public final static String CUSTOMER_CODE_PRE = "CC";
    //客户服务工单编码key
    public final static String WO_CUST_SER_CODE_PRE = "";
    //问题件工单编码
    public final static String WO_PROBLEM_CODE_PRE = "WT";
    //邮政投诉工单编码key
    public final static String WO_POSTAL_COMP_CODE_PRE = "YZ";
    //仲裁投诉工单编码key
    public final static String WO_ARB_COMP_CODE_PRE = "TS";
    //仲裁申诉工单编码key
    public final static String WO_ARB_APPEAL_CODE_PRE = "SS";
    //电商投诉工单编码key
    public final static String WO_ORE_COMP_CODE_PRE = "DS";
    //电商申诉工单编码key
    public final static String WO_ORE_APPEAL_CODE_PRE = "DSSS";
    //客户理赔工单编码key
    public final static String WO_CUSTOMER_SETTLEMENT_PRE = "KHLP";
    //保险理赔工单编码key
    public final static String WO_INSURRANCE_CODE_PRE ="INS";
    //订单编码key
    public final static String ORDER_CODE_PRE ="CS";

    //时间格式化
    public final static String DATE_FORMAT_YYMMDD = "yyMMdd";
    public final static String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";

    public static String getCode(String key){
        //根据缓存key获取缓存
        ICache<String, String> iCache = CacheManager.getInstance().getCache("CcCodeCache");
        String code = iCache.get(key);
        String newCode = dealWithCode(code,key);
        //将新的数据放到缓存中
        CcCodeCache CcCodeCache = CacheManager.getInstance().getCache(CcCodeCache.class,"CcCodeCache");
        CcCodeCache.set(key,newCode);
        return newCode;
    }


    private static String dealWithCode(String value, String key){
        //判断获取当前的编码是否为空
        if (StringUtil.isEmpty(value) || "null".equalsIgnoreCase(value)) {
            String dateformat = dealWithDate(key);
            if(CcCodeCache.CUSTOMER_KEY.equals(key)){//客户
                String customerNum = Constants.FIVE_ZERO_ONE;
                return CUSTOMER_CODE_PRE+dateformat + customerNum;
            }else if(CcCodeCache.WO_CUST_SER_CODE_KEY.equals(key)){//客服工单
                String custSerNum = Constants.SIX_ZERO_ONE;
                return WO_CUST_SER_CODE_PRE + dateformat + custSerNum;
            }else if(CcCodeCache.WO_PROBLEM_CODE_KEY.equals(key)){//问题件
                String custSerNum = Constants.SIX_ZERO_ONE;
                return WO_PROBLEM_CODE_PRE + dateformat + custSerNum;
            }else if(CcCodeCache.WO_POSTAL_COMP_CODE_KEY.equals(key)){//邮政投诉工单
                String customerNum = Constants.FOR_ZERO_ONE;
                return WO_POSTAL_COMP_CODE_PRE+dateformat + customerNum;
            }else if(CcCodeCache.WO_ARB_COMP_CODE_KEY.equals(key)){//仲裁投诉工单
                String customerNum = Constants.FOR_ZERO_ONE;
                return WO_ARB_COMP_CODE_PRE+dateformat + customerNum;
            }else if(CcCodeCache.WO_ARB_APPEAL_CODE_KEY.equals(key)){//仲裁申诉工单
                String customerNum = Constants.FOR_ZERO_ONE;
                return WO_ARB_APPEAL_CODE_PRE+dateformat + customerNum;
            }else if(CcCodeCache.WO_ORE_COMP_CODE_KEY.equals(key)){//电商投诉工单
                String customerNum = Constants.FOR_ZERO_ONE;
                return WO_ORE_COMP_CODE_PRE+dateformat + customerNum;
            }else if(CcCodeCache.WO_ORE_APPEAL_CODE_KEY.equals(key)){//电商申诉工单
                String customerNum = Constants.FOR_ZERO_ONE;
                return WO_ORE_APPEAL_CODE_PRE+dateformat + customerNum;
            }else if(CcCodeCache.WO_CUSTOMER_SETTLEMENT_CODE_KEY.equals(key)){
                //客户理赔工单
                String customerNum = Constants.FOR_ZERO_ONE;
                return WO_CUSTOMER_SETTLEMENT_PRE + dateformat + customerNum;
            }else if(CcCodeCache.WO_INSURRANCE_CODE_KEY.equals(key)){
                //保险理赔工单
                String customerNum = Constants.FOR_ZERO_ONE;
                return WO_INSURRANCE_CODE_PRE + dateformat + customerNum;
            } else if(CcCodeCache.ORDER_CODE_KEY.equals(key)){
                String customerNum = Constants.FOR_ZERO_ONE;
                return ORDER_CODE_PRE+dateformat + customerNum;
            }
            //文件导出
            else if(CcCodeCache.EXPORT_FILE.equals(key)){
                String customerNum = Constants.FIVE_ZERO_ONE;
                return dateformat + customerNum;
            } else {
                return null;
            }
        } else {
            String subDate  = dealWithRadisValueDate(value,key);//获取截取的时间
            String nowdate = dealWithDate(key);//当前时间
            if(subDate.equals(nowdate)){
                if(CcCodeCache.CUSTOMER_KEY.equals(key)){//客户
                    return CUSTOMER_CODE_PRE + getResult(CUSTOMER_CODE_PRE,value);
                }else if(CcCodeCache.WO_CUST_SER_CODE_KEY.equals(key)){//客服工单
                    return WO_CUST_SER_CODE_PRE + getResult(WO_CUST_SER_CODE_PRE,value);
                }else if(CcCodeCache.WO_PROBLEM_CODE_KEY.equals(key)){//问题件
                    return WO_PROBLEM_CODE_PRE + getResult(WO_PROBLEM_CODE_PRE,value);
                }else if(CcCodeCache.WO_POSTAL_COMP_CODE_KEY.equals(key)){//邮政投诉工单
                    return WO_POSTAL_COMP_CODE_PRE + getResult(WO_POSTAL_COMP_CODE_PRE,value);
                }else if(CcCodeCache.WO_ARB_COMP_CODE_KEY.equals(key)){//仲裁投诉工单
                    return WO_ARB_COMP_CODE_PRE + getResult(WO_ARB_COMP_CODE_PRE,value);
                }else if(CcCodeCache.WO_ARB_APPEAL_CODE_KEY.equals(key)){//仲裁申诉工单
                    return WO_ARB_APPEAL_CODE_PRE + getResult(WO_ARB_APPEAL_CODE_PRE,value);
                }else if(CcCodeCache.WO_ORE_COMP_CODE_KEY.equals(key)){//电商投诉工单
                    return WO_ORE_COMP_CODE_PRE + getResult(WO_ORE_COMP_CODE_PRE,value);
                }else if(CcCodeCache.WO_ORE_APPEAL_CODE_KEY.equals(key)){//电商申诉工单
                    return WO_ORE_APPEAL_CODE_PRE + getResult(WO_ORE_APPEAL_CODE_PRE,value);
                }else if(CcCodeCache.WO_CUSTOMER_SETTLEMENT_CODE_KEY.equals(key)){
                    //客户理赔工单
                    return WO_CUSTOMER_SETTLEMENT_PRE + getResult(WO_CUSTOMER_SETTLEMENT_PRE, value);
                }else if(CcCodeCache.WO_INSURRANCE_CODE_KEY.equals(key)){
                    //保险理赔工单
                    return WO_INSURRANCE_CODE_PRE + getResult(WO_INSURRANCE_CODE_PRE, value);
                } else if(CcCodeCache.ORDER_CODE_KEY.equals(key)){
                    return ORDER_CODE_PRE+ getResult(ORDER_CODE_PRE,value);
                }//文件导出
                else if(CcCodeCache.EXPORT_FILE.equals(key)){
                    return getResult("",value);
                }
                else{
                    return null;
                }
            }else{
                if(CcCodeCache.CUSTOMER_KEY.equals(key)){//客户
                    return CUSTOMER_CODE_PRE + nowdate + Constants.FIVE_ZERO_ONE;
                }else if(CcCodeCache.WO_CUST_SER_CODE_KEY.equals(key)){//客服工单
                    return WO_CUST_SER_CODE_PRE + nowdate + Constants.SIX_ZERO_ONE;
                }else if(CcCodeCache.WO_PROBLEM_CODE_KEY.equals(key)){//问题件
                    return WO_PROBLEM_CODE_PRE + nowdate + Constants.SIX_ZERO_ONE;
                }else if(CcCodeCache.WO_POSTAL_COMP_CODE_KEY.equals(key)){//邮政投诉工单
                    return WO_POSTAL_COMP_CODE_PRE + nowdate + Constants.FOR_ZERO_ONE;
                }else if(CcCodeCache.WO_ARB_COMP_CODE_KEY.equals(key)){//仲裁投诉工单
                    return WO_ARB_COMP_CODE_PRE + nowdate + Constants.FOR_ZERO_ONE;
                }else if(CcCodeCache.WO_ARB_APPEAL_CODE_KEY.equals(key)){//仲裁申诉工单
                    return WO_ARB_APPEAL_CODE_PRE + nowdate + Constants.FOR_ZERO_ONE;
                }else if(CcCodeCache.WO_ORE_COMP_CODE_KEY.equals(key)){//电商投诉工单
                    return WO_ORE_COMP_CODE_PRE + nowdate + Constants.FOR_ZERO_ONE;
                }else if(CcCodeCache.WO_ORE_APPEAL_CODE_KEY.equals(key)){//电商申诉工单
                    return WO_ORE_APPEAL_CODE_PRE + nowdate + Constants.FOR_ZERO_ONE;
                }else if(CcCodeCache.WO_CUSTOMER_SETTLEMENT_CODE_KEY.equals(key)){//客户理赔
                    return WO_CUSTOMER_SETTLEMENT_PRE + nowdate + Constants.FOR_ZERO_ONE;
                }else if(CcCodeCache.WO_INSURRANCE_CODE_KEY.equals(key)){
                    return WO_INSURRANCE_CODE_PRE + nowdate + Constants.FOR_ZERO_ONE;
                } else if(CcCodeCache.ORDER_CODE_KEY.equals(key)){
                    return ORDER_CODE_PRE+ nowdate + Constants.FOR_ZERO_ONE;
                    //文件导出
                } else if(CcCodeCache.EXPORT_FILE.equals(key)){
                    return nowdate + Constants.FOR_ZERO_ONE;
                }
                else{
                    return null;
                }
            }
        }
    }
    private static String dealWithDate(String key) {
        String date = null;
        if (CcCodeCache.CUSTOMER_KEY.equals(key)) {//客户
            SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT_YYMMDD);//设置日期格式
            String customerDate = df.format(new Date());
            date = customerDate;
        } else {
            SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT_YYYYMMDD);//设置日期格式
            String workorderDate = df.format(new Date());
            date = workorderDate;
        }
        return date;
    }
    private static String dealWithRadisValueDate(String value, String key) {
        if(CcCodeCache.CUSTOMER_KEY.equals(key)){//客户
            return value.substring(CUSTOMER_CODE_PRE.length(),CUSTOMER_CODE_PRE.length()+DATE_FORMAT_YYMMDD.length());

        }else if(CcCodeCache.WO_CUST_SER_CODE_KEY.equals(key)){//客服工单
            return value.substring(WO_CUST_SER_CODE_PRE.length(),WO_CUST_SER_CODE_PRE.length()+DATE_FORMAT_YYYYMMDD.length());

        }else if(CcCodeCache.WO_PROBLEM_CODE_KEY.equals(key)){//问题件
            return value.substring(WO_PROBLEM_CODE_PRE.length(),WO_PROBLEM_CODE_PRE.length()+DATE_FORMAT_YYYYMMDD.length());

        }else if(CcCodeCache.WO_POSTAL_COMP_CODE_KEY.equals(key)){//邮政投诉工单
            return value.substring(WO_POSTAL_COMP_CODE_PRE.length(),WO_POSTAL_COMP_CODE_PRE.length()+DATE_FORMAT_YYYYMMDD.length());

        }else if(CcCodeCache.WO_ARB_COMP_CODE_KEY.equals(key)){//仲裁投诉工单
            return value.substring(WO_ARB_COMP_CODE_PRE.length(),WO_ARB_COMP_CODE_PRE.length()+DATE_FORMAT_YYYYMMDD.length());

        }else if(CcCodeCache.WO_ARB_APPEAL_CODE_KEY.equals(key)){//仲裁申诉工单
            return value.substring(WO_ARB_APPEAL_CODE_PRE.length(),WO_ARB_APPEAL_CODE_PRE.length()+DATE_FORMAT_YYYYMMDD.length());
        }else if(CcCodeCache.WO_ORE_COMP_CODE_KEY.equals(key)){//电商投诉工单
            return value.substring(WO_ORE_COMP_CODE_PRE.length(),WO_ORE_COMP_CODE_PRE.length()+DATE_FORMAT_YYYYMMDD.length());
        }else if(CcCodeCache.WO_ORE_APPEAL_CODE_KEY.equals(key)){//电商申诉工单
            return value.substring(WO_ORE_APPEAL_CODE_PRE.length(),WO_ORE_APPEAL_CODE_PRE.length()+DATE_FORMAT_YYYYMMDD.length());
        }else if(CcCodeCache.WO_CUSTOMER_SETTLEMENT_CODE_KEY.equals(key)){//客服理赔工单
            return value.substring(WO_CUSTOMER_SETTLEMENT_PRE.length(),WO_CUSTOMER_SETTLEMENT_PRE.length() + DATE_FORMAT_YYYYMMDD.length());
        }else if(CcCodeCache.WO_INSURRANCE_CODE_KEY.equals(key)){//保险理赔工单
            return value.substring(WO_INSURRANCE_CODE_PRE.length(),WO_INSURRANCE_CODE_PRE.length() + DATE_FORMAT_YYYYMMDD.length());
        }else if(CcCodeCache.ORDER_CODE_KEY.equals(key)){//订单
            return value.substring(ORDER_CODE_PRE.length(),ORDER_CODE_PRE.length()+DATE_FORMAT_YYYYMMDD.length());
        }
        else{
            return "";
        }
    }

    private static String getResult(String pre,String value){
        String code = value.substring(pre.length());
        long codeValues = Long.valueOf(code);
        return Long.toString(codeValues+1);
    }
}
